﻿using System;
using System.Web.UI;
using Microsoft.SharePoint;

namespace Eneta.Portal.NewsAndBlogs.Pages
{
    public class BlogPostView : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var idString = Request.QueryString["ID"];
            int id;

            if (!int.TryParse(idString, out id))
            {
                Response.Redirect("/uudised/blogid/");
                return;
            }

            var siteId = SPContext.Current.Site.ID;
            var pageName = string.Empty;

            var sec = new SPSecurity.CodeToRunElevated(
                delegate
                {
                    var pageId = 0;

                    using (var site = new SPSite(siteId))
                    {
                        using (var web = site.OpenWeb())
                        {
                            var list = web.Lists["FeedEntries"];
                            var queryString =
                                "<Where><Eq><FieldRef Name='ID' /><Value Type='int'>{0}</Value></eq></Where>";
                            queryString = string.Format(queryString, id);

                            var query = new SPQuery { Query = queryString, RowLimit = 1 };

                            var items = list.GetItems(query);
                            if (items == null)
                                return;
                            if (items.Count == 0)
                                return;

                            var item = items[0];
                            if (item["NewPageId"] == null)
                                return;
                            pageId = int.Parse(item["NewPageId"].ToString());
                        }

                        if (pageId == 0)
                            return;

                        using (var web = site.OpenWeb("/uudised/blogid"))
                        {
                            var list = web.Lists["Lehed"];
                            var queryString =
                                "<Where><Eq><FieldRef Name='ID' /><Value Type='int'>{0}</Value></eq></Where>";
                            queryString = string.Format(queryString, pageId);

                            var query = new SPQuery { Query = queryString, RowLimit = 1 };

                            var items = list.GetItems(query);
                            if (items == null)
                                return;
                            if (items.Count == 0)
                                return;

                            var item = items[0];
                            var field = item.Fields.GetFieldByInternalName("FileLeafRef");
                            pageName = item[field.Id].ToString();
                        }
                    }
                }
                );
            SPSecurity.RunWithElevatedPrivileges(sec);

            if (!string.IsNullOrEmpty(pageName))
            {
                Response.Redirect("/uudised/blogid/Lehed/" + pageName);
                return;
            }
            Response.Redirect("/uudised/blogid/");

        }
    }
}
